﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



/// <summary>
/// 用于动态创建名字空间的构建器。
/// </summary>
public class NamespaceBuilder : MemberBuilder {

    /// <summary>
    /// 获取当前成员的所有子成员。
    /// </summary>
    public SingleLinkedList<MemberBuilder> members = new SingleLinkedList<MemberBuilder>();

    public sealed override void generateID(WriteContext context) {
        id = context.currentId++;
        foreach (MemberBuilder member in members) {
            member.id = context.currentId++;
            member.generateID(context);
        }
    }

    public override void write(WriteContext context) {

        if (members.isEmpty) {
            attributes |= MemberAttributes.empty;
        }

        base.write(context);

        if (members.isEmpty) {
            return;
        }

        // 写入子成员信息。
        context.writeMember((MemberBuilder)members.first);

        // 写入子成员。
        var prevParentMember = context.parentMember;
        context.parentMember = this;

        // 第2步：写入当前成员的直接子成员。
        foreach (var member in members) {
            member.write(context);
        }

        context.parentMember = prevParentMember;

    }

    public sealed override void writeIndex(WriteContext context) {
        foreach (MemberBuilder member in members) {
            context.writeInt(member.offset);
            member.writeIndex(context);
        }
    }

}